package de.blinkt.openvpn.advancedconnection;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.preference.DialogPreference;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import de.blinkt.openvpn.R;

public class NetworkListPreference extends DialogPreference implements NetworkListPreferenceView {

	private Button mAddButton;
	private Button mRemoveButton;
	private ListView mNetworkList;
	private ArrayAdapter<String> mListAdapter;
	private NetworkListPreferencePresenter mPresenter;

	public NetworkListPreference(Context context, AttributeSet attrs) {
		super(context, attrs);
		
		mPresenter = new NetworkListPreferencePresenter(this, (Activity)context);

		//set layouts
//		setDialogLayoutResource(R.layout.network_list_preference);
		
	}
	
	 @Override
	protected void onDialogClosed(boolean positiveResult) {
		super.onDialogClosed(positiveResult);
		if(positiveResult){
			mPresenter.listSaved();
		}
	}
	


	@Override
	protected View onCreateDialogView() {
		View view =  super.onCreateDialogView();
		
		mAddButton = (Button)view.findViewById(R.id.addCurrentNetworkButton);
//		mRemoveButton = (Button)view.findViewById(R.id.removeSelectedButton);
		mNetworkList = (ListView)view.findViewById(R.id.networkList);
		
		mPresenter.initView();
		
		return view;
	}
	
	
	
	public ArrayAdapter<String> getListAdapter() {
		return mListAdapter;
	}
	
	@Override
	public Button getAddButton() {
		return mAddButton;
	}
	
	@Override
	public Button getRemoveButton() {
		
		return mRemoveButton;
	}
	
	@Override
	public ListView getNetworkList() {
		return mNetworkList;
	}
	
	public NetworkListPreferencePresenter getPresenter() {
		return mPresenter;
	}

	@Override
	public void showUnableToAddNetworkError() {
		
		
		new AlertDialog.Builder(getContext()).
		setCancelable(false).
		setMessage("Unable to add current network. It is not a supported network type").
		setTitle("Unable to Add Network").
		setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				
			}
		}).
		
		show();
		
	}
	

}
